package com.erp.produce.controller;


import com.erp.common.ResponseMessage;
import com.erp.common.controller.BaseController;
import com.erp.common.util.TimeUtil;
import com.erp.produce.entity.Order;
import com.erp.produce.service.OrderService;
import com.erp.stock.entity.RawMaterial;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("订单信息接口")
@RestController
@RequestMapping("/order")
public class OrderController implements BaseController<Order> {

    @Autowired
    private OrderService orderService;

    @ApiOperation("改变订单状态")
    @PostMapping("/status/{status}")
    public ResponseMessage changeOrderStatus(@PathVariable("status") Integer status, Long id){
        if (orderService.changeStatus(status,id)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("订单状态更新失败");
    }

    @ApiOperation("返回所有订单信息")
    @GetMapping("/list")
    //get请求，返回订单列表
    public ResponseMessage<List<Order>> orderList(){
        return new ResponseMessage<>(orderService.getOrderList());
    }

    @ApiOperation("新增订单信息")
    //post请求，添加订单，成功返回true，失败返回false
    @PostMapping("/add")
    public ResponseMessage add(Order order){
        //System.out.println(order);
        order.setOrderStartTime(TimeUtil.now());
        order.setOrderStatus(0);
        if (order.getGoodInfo().isEmpty()) {
            return ResponseMessage.fail("未添加产品");
        }
        if (order.getCustomerId() == null) {
            return ResponseMessage.fail("未填入客户");
        }
        Boolean ok = orderService.addOrder(order);
        if (ok) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("订单保存失败");
    }

    @ApiOperation("删除订单信息（支持多选）")
    @Override
    @PostMapping("list/delete")
    public ResponseMessage deleteSome(@RequestParam(value = "idArray[]")Long[] ids) {
        if (orderService.deleteIds(ids)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("删除订单失败");
    }

    @ApiOperation("更新订单信息")
    @Override
    @PostMapping("/update")
    public ResponseMessage update(Order entity) {
        if (orderService.update(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("更新订单数据失败，请重试");
    }

    @ApiOperation("分页展示订单信息")
    @GetMapping("/list/{page}/{size}")
    @Override
    public ResponseMessage listPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        List<Order> orders = orderService.listPage(page, size);
        if (orders.size()>0){
            return new ResponseMessage(orders);
        }
        return ResponseMessage.fail("无订单信息");
    }

    @ApiOperation("分页展示订单信息（支持多选）")
    @Override
    @GetMapping("list/name/{page}/{size}")
    public ResponseMessage listPageLike(@PathVariable("page") int page, @PathVariable("size") int size, String name) {
        List<Order> orders = orderService.listPageLike(page, size, name);
        if (orders.size()>0){
            return new ResponseMessage(orders);
        }

        return ResponseMessage.fail("无订单信息");
    }


    @ApiOperation(value = "制定生产工艺接口")
    @PostMapping("/establishPT")
    public ResponseMessage establishProductionTechnology(Long orderId,String ptInfo){
        Order order = new Order();
        order.setId(orderId);
        order.setGoodInfo(ptInfo);
        order.setOrderStatus(1);
        if (orderService.saveProductionTechnology(order)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("提交失败，请重试");
    }

    @PostMapping("/makeprodplan")
    @ApiOperation(value = "制定生产计划接口")
    public ResponseMessage makeProdPlan(Long orderId){
        List<String> messages = orderService.calculateRawMaterialStock(orderId);
        return new ResponseMessage(messages);
    }

    @GetMapping("/rm/list")
    @ApiOperation("根据订单获取原材料列表")
    public ResponseMessage rawMaterialList(Long orderId){
        List<RawMaterial> rmByOrderId = orderService.getRMByOrderId(orderId);
        if (rmByOrderId.size() == 0) {
            return ResponseMessage.fail("无更多信息");
        }
        return new ResponseMessage(rmByOrderId);
    }
}
